package com.jbinfo.qingcaowan.studyManager.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.bana.common.util.page.PageCond;
import org.bana.common.util.page.PageCondUtil;
import org.bana.common.util.page.PageResult;
import org.springframework.stereotype.Service;

import com.jbinfo.common.utils.DataTables;
import com.jbinfo.common.utils.UUIDUtils;
import com.jbinfo.qingcaowan.common.BusinessException;
import com.jbinfo.qingcaowan.common.ClonePojoUtil;
import com.jbinfo.qingcaowan.common.ExceptionConstants.BusinessExceptionCode;
import com.jbinfo.qingcaowan.security.entity.User;
import com.jbinfo.qingcaowan.studyManager.dao.ReadingNoteDao;
import com.jbinfo.qingcaowan.studyManager.dto.NoteDTO;
import com.jbinfo.qingcaowan.studyManager.dto.NoteInfoModel;
import com.jbinfo.qingcaowan.studyManager.entity.BookCard;
import com.jbinfo.qingcaowan.studyManager.entity.NoteInfo;
import com.jbinfo.qingcaowan.studyManager.service.ReadingNoteService;
import com.jbinfo.qingcaowan.user.dao.ScoreDetailDAO;
import com.jbinfo.qingcaowan.user.dao.ScoreSetDAO;
import com.jbinfo.qingcaowan.user.dao.UserDao;
import com.jbinfo.qingcaowan.user.domain.ScoreDetailDomain;
/**
 * 
* @ClassName: UserServiceImpl 
* @Description: TODO 用户管理service实现类
* @author ZhangLei 
* @date 2016年6月22日 上午10:23:02
 */
@Service("readingNoteService")
public class ReadingNoteServiceImpl implements ReadingNoteService {

	@Resource(name="readingNoteDao")
	private ReadingNoteDao readingNoteDao;
	
	@Resource(name="scoreSetDAO")
	private ScoreSetDAO scoreSetDAO;
	
	@Resource(name="userDao")
	private UserDao userDao;
	
	@Resource(name="scoreDetailDAO")
	private ScoreDetailDAO scoreDetailDAO;
	
	
	@Override
	public List<NoteInfo> getByCondition(String condition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<NoteInfo> findAll() {
		
		return readingNoteDao.findAll();
	}

	@Override
	public DataTables<NoteInfo> findByParam(NoteInfoModel noteInfoModel) {
		List<NoteInfo> tList= readingNoteDao.findByParam(noteInfoModel);
		return DataTables.combinTotalRecords(tList);
	}

	@Override
	public Object getById(String noteNum) {
		return (NoteInfo) readingNoteDao.selectByPrimaryKey(noteNum);
	}

	@Override
	public void update(String noteNum) {
		readingNoteDao.updateBynoteNum(noteNum);
		
	}

	@Override
	public List<NoteInfoModel> findAllbook() {
		return readingNoteDao.findAllbook();
	}

	@Override
	public List<NoteInfoModel> findAllChapter(String bookId) {
		return readingNoteDao.findAllChapter(bookId);
	}

	@Override
	public List<NoteInfoModel> findAllCard(String chapterId) {
		return readingNoteDao.findAllCard(chapterId);
	}

	@Override
	public void toInvalid(String noteNum) {
		readingNoteDao.toInvalid(noteNum);
		
	}

	@Override
	public List<BookCard> findCardList(BookCard bookCard) {
		return readingNoteDao.findCardList(bookCard);
	}

	// 保存笔记信息
	@Override
	public Object saveNoteInfo(NoteInfo noteInfo) {
		
		if (noteInfo.getNoteAuthor() == null 
				|| noteInfo.getNoteAuthor().isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.param_error, "用户ID不能为空");
		}
		// 根据noteNum 非空来判断是 插入还是更新
		if (noteInfo.getNoteNum() == null || noteInfo.getNoteNum().equals("")) {
			noteInfo.setNoteNum(UUIDUtils.create());
			noteInfo.setNoteDate(new Date());
			noteInfo.setNoteStatus("1");
			
			readingNoteDao.insertSelective(noteInfo);
			
		} else {
			// 更新笔记信息
			readingNoteDao.updateByPrimaryKeySelective(noteInfo);
		}
		
		// 根据userId 获取orgId
		String userId = noteInfo.getNoteAuthor();
		String orgId = "";
		User user = (User) userDao.selectByPrimaryKey(userId);
		orgId = user.getUserOrg();
		
		// 插入学分详情表
		String type = "0004";
		Integer score = scoreSetDAO.findScoreByType(type);
		
		ScoreDetailDomain entity = new ScoreDetailDomain();
		entity.setId(UUIDUtils.create());
		entity.setType(type);
		entity.setOrgId(orgId);
		entity.setUserId(userId);
		entity.setScore(score);
		entity.setCreatUser(userId);
		entity.setCreatTime(new Date());
		entity.setModifyUser(userId);
		entity.setModifyTime(new Date());
		
		scoreDetailDAO.save(entity);
		return null;
	}

	// 获取笔记数
	@Override
	public Object getNoteCnt(NoteInfo noteInfo) {
		String strCnt = "";
		
		Integer nCnt = readingNoteDao.findNoteCntByParam(noteInfo);
		strCnt = nCnt.toString();
		
		return strCnt;
	}
	
	// 分页查询笔记列表
	@Override
	public Object findNoteListByPage(NoteDTO noteDTO, 
			Integer currentPage, Integer pageSize) {
		
		List<NoteDTO> dtoList = new ArrayList<NoteDTO>();
		
		PageCond pageCond = new PageCond(currentPage,pageSize);
		
		PageCond pageCondQuery = PageCondUtil.check(pageCond);
		
		List<NoteDTO> noteList = readingNoteDao.findNoteListByPage(noteDTO, pageCondQuery);
		
		if (noteList == null || noteList.isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.database_none_data, "无笔记数据");
		}
		
		NoteDTO dto = new NoteDTO();
		
		for (NoteDTO note: noteList) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String strNoteDate = sdf.format(note.getNoteDate());
			
			dto = ClonePojoUtil.copyClassFromTo(note, NoteDTO.class);
			dto.setStrNoteDate(strNoteDate);
			
			dtoList.add(dto);
		}
		
		Integer nNoteCnt = readingNoteDao.findNoteCnt(noteDTO);
		
		pageCondQuery.setTotalCount(nNoteCnt);
		pageCondQuery.setPageCount(PageCondUtil.calculatePageCount(pageCondQuery));
		
		return new PageResult<NoteDTO>(dtoList, pageCondQuery);
	}

	// 根据笔记序号查询笔记详情
	@Override
	public Object findNoteByNum(NoteDTO noteDTO) {
		String noteNum = noteDTO.getNoteNum();
		NoteInfo noteInfo = readingNoteDao.selectByPrimaryKey(noteNum);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date dNoteDate = noteInfo.getNoteDate();
		String strNoteDate = sdf.format(dNoteDate);
		noteInfo.setStrNoteDate(strNoteDate);
		
		return noteInfo;
	}

	// 根据读者、书籍、章节、卡片查询笔记信息
	@Override
	public Object findNoteInfoByParam(NoteDTO noteDTO) {

		NoteInfo noteInfo = ClonePojoUtil.copyClassFromTo(noteDTO, NoteInfo.class);
		List<NoteInfo> noteList = readingNoteDao.findNoteInfoByParam(noteInfo);
		NoteInfo note = null;
		if (noteList != null && !noteList.isEmpty()) {
			note = noteList.get(0);
		}
		return note;
	}
	
	
	

}
